package nero.yncc.com.createart.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import nero.yncc.com.createart.R;
import nero.yncc.com.createart.adapter.AdapterSearchHistory;
import nero.yncc.com.createart.adapter.AdapterSearchTuijian;
import nero.yncc.com.createart.base.BaseActivity;
import nero.yncc.com.createart.bean.BeanType;
import nero.yncc.com.createart.bean.BeanTypeIn;
import nero.yncc.com.createart.custom.IconView;
import nero.yncc.com.createart.custom.MyRecyclerView;
import nero.yncc.com.createart.utils.DialogUtil;
import nero.yncc.com.createart.utils.MyLayoutManager;
import nero.yncc.com.createart.utils.TextUtil;

import static android.view.View.GONE;

/**
 * Created by nero on 17-6-26.
 */

@ContentView(R.layout.activity_search)
public class ActivitySearch extends BaseActivity {

    @ViewInject(R.id.tv_search)
    TextView tv_search;
    @ViewInject(R.id.rc_history)
    MyRecyclerView rc_history;
    @ViewInject(R.id.rc_tuijian)
    MyRecyclerView rc_tuijian;
    @ViewInject(R.id.tv_no_history)
    TextView tv_no_history;
    @ViewInject(R.id.tv_no_tuijian)
    TextView tv_no_tuijian;
    @ViewInject(R.id.et_search)
    EditText et_search;

    ArrayList<String> arrayList_history; //从本地读取

    ArrayList<BeanTypeIn> arrayList_tuijian;

    AdapterSearchHistory adapterSearchHistory;
    AdapterSearchTuijian adapterSearchTuijian;

    @ViewInject(R.id.icon_junk)
    IconView icon_junk;

    @Override
    protected void initView() {
        super.initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getHistory();
    }

    @Override
    protected void initData() {
        super.initData();
        rc_history.setLayoutManager(new MyLayoutManager(mContext));
        arrayList_history = new ArrayList<>();
        adapterSearchHistory = new AdapterSearchHistory(mContext, arrayList_history);
        rc_history.setAdapter(adapterSearchHistory);


        rc_tuijian.setLayoutManager(new MyLayoutManager(mContext));
        arrayList_tuijian = new ArrayList<>();
        arrayList_tuijian.add(new BeanTypeIn("测试推荐1", true, true));
        arrayList_tuijian.add(new BeanTypeIn("测试", true, true));
        arrayList_tuijian.add(new BeanTypeIn("测试推1", true, true));
        arrayList_tuijian.add(new BeanTypeIn("测试推荐推荐推荐推荐推荐", true, true));
        arrayList_tuijian.add(new BeanTypeIn("测试推荐推荐", true, true));
        adapterSearchTuijian = new AdapterSearchTuijian(mContext, arrayList_tuijian);
        rc_tuijian.setAdapter(adapterSearchTuijian);
        getHistory();
    }
    String history;
    SharedPreferences sharedPreferences_history;
    private void getHistory() {
        arrayList_history.clear();
        sharedPreferences_history= getSharedPreferences("history", MODE_PRIVATE);
        history = sharedPreferences_history.getString("history", "");
        String[] historyArr = history.split(",");
        for (String historyCache : historyArr) {
            if (!TextUtils.isEmpty(historyCache))
                arrayList_history.add(historyCache);
        }
        if (arrayList_history.size() <= 0) {
            tv_no_history.setVisibility(View.VISIBLE);
            icon_junk.setVisibility(GONE);
        } else {
            tv_no_history.setVisibility(GONE);
            icon_junk.setVisibility(View.VISIBLE);
            adapterSearchHistory.notifyDataSetChanged();
        }
    }

    @Override
    protected void getHttpData() {
        super.getHttpData();
        if(arrayList_tuijian.size()<=0){
            tv_no_tuijian.setVisibility(View.VISIBLE);
        }else{
            tv_no_tuijian.setVisibility(View.GONE);
        }
    }

    @Override
    protected void initListener() {
        super.initListener();
        tv_search.setOnClickListener(this);
        icon_junk.setOnClickListener(this);
    }

    @Override
    protected void doClick(int id) {
        super.doClick(id);
        switch (id){
            case R.id.tv_search:
                search(et_search.getText().toString());
                break;
            case R.id.icon_junk:
                DialogUtil.getConfirmDialog(mContext, "是否清空历史记录？", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        showProgress("正在清空历史纪录...");
                        sharedPreferences_history.edit().putString("history","").commit();
                        new Timer().schedule(new TimerTask() {
                            @Override
                            public void run() {
                               runOnUiThread(new Runnable() {
                                   @Override
                                   public void run() {
                                       hideProgress();
                                       showToast("清楚成功");
                                       arrayList_history.clear();
                                       adapterSearchHistory.notifyDataSetChanged();
                                       icon_junk.setVisibility(GONE);
                                   }
                               });
                            }
                        },2000);
                    }
                }).show();
                break;
        }
    }

    private void search(String str){
        if(!TextUtils.isEmpty(str) ){
            if(history.indexOf(","+str+",")==-1){
                //写入本地历史纪录
                if(history.endsWith(",")){
                    history+=str+",";
                }else{
                    history+=str+",";
                }
                sharedPreferences_history.edit().putString("history",history).commit();
            }

            Intent intent=new Intent(mContext,ActivitySearchAllGod.class);
            intent.putExtra("keyword",str);
            startActivity(intent);
        }else{
            Intent intent=new Intent(mContext,ActivitySearchAllGod.class);
            startActivity(intent);
        }

    }
}
